package com.website.company.controller;


import com.website.company.entity.Notice;
import com.website.company.entity.Product;
import com.website.company.service.NoticeService;
import com.website.company.service.ProductFunctionRelationService;
import com.website.company.service.ProductService;
import com.website.company.utils.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author peng yangyan
 * @since 2018-11-22
 */
@RestController
@RequestMapping("/api/notice")
public class NoticeController {

    @Autowired
    NoticeService noticeService;
   
    @GetMapping("/getNotice")
    ResponseBo getNotice(@RequestParam("id") Integer id) {
        try {
            NoticeVO productVO = new NoticeVO();
            Notice product = noticeService.show(id);
            return ResponseBo.ok(product);
        } catch (Exception e) {
            return ResponseBo.error(e.getMessage());
        }
    }

    @GetMapping("/getList")
    ResponseList<Notice> getList(@RequestParam(value = "page", defaultValue = "1") Integer page, @RequestParam(value = "rows", defaultValue = "10") Integer rows,
                                  @RequestParam("keyword") String keyword) {
        return noticeService.list(page, rows, keyword, null);
    }

    @PostMapping("/delete")
    ResponseBo delete(@RequestBody Integer[] ids) {
        try {
            noticeService.delete(ids);
            return ResponseBo.ok();
        } catch (Exception e) {
            return ResponseBo.error(e.getMessage());
        }
    }

    @PostMapping("/saveNotice")
    ResponseBo saveNotice(@RequestBody NoticeVO productVO) {
        if (productVO.getId() == null) {
            try {
                Notice product = new Notice();
                BeanUtils.copyProperties(productVO, product);
                noticeService.add(product);
                return ResponseBo.ok(product);
            } catch (Exception e) {
                return ResponseBo.error(e.getMessage());
            }
        } else {
            try {
                Notice product = new Notice();
                BeanUtils.copyProperties(productVO, product);
                noticeService.update(product);
                return ResponseBo.ok(product);
            } catch (Exception e) {
                return ResponseBo.error(e.getMessage());
            }
        }
    }

}
